﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.Reflection;
using Metamorph.Configuration;
using System.Collections.Generic;

namespace Metamorph.Core
{
	public class ActionDispatcher : IActionDispatcher
	{
		private static ActionDispatcher adInstance;

		public delegate void ActionDelegate();
		public delegate void MessageDelegate(object oMessage);
		public delegate void ExceptionDelegate(Exception eException);

		private Dictionary<string, ActionDelegate>		dActions;
		private Dictionary<string, MessageDelegate>		dMessageActions;
		private Dictionary<string, ExceptionDelegate>	dExceptionActions;

		private ActionDispatcher()
		{
			dActions			= new Dictionary<string, ActionDelegate>();
			dMessageActions		= new Dictionary<string, MessageDelegate>();
			dExceptionActions	= new Dictionary<string, ExceptionDelegate>();
		}

		public static ActionDispatcher Instance
		{
			get
			{
				if (adInstance == null)
				{
					adInstance = new ActionDispatcher();
				}

				return adInstance;
			}
        }

		#region IActionDispatcher Members

		public void Execute(string sActionName, IProcessContext pcProcessContext)
		{
			if (sActionName.Contains("."))
			{
				IAction aAction = (IAction)AssemblyUtils.Instance.GetCodeAssembly().CreateInstance(sActionName);

				if (aAction == null)
				{
					throw new Exception(sActionName + " class not found in assembly " + AssemblyUtils.Instance.GetCodeAssembly().FullName + ".");
				}

				aAction.StateMachineContext = pcProcessContext;
				aAction.Execute();
			}
			else
			{
				if (!dActions.ContainsKey(sActionName))
				{
					if (pcProcessContext == null)
					{
						throw new Exception("The process has no context associated.");
					}

					MethodInfo miMethodInfo = pcProcessContext.GetType().GetMethod(sActionName);

					if (miMethodInfo == null)
					{
						throw new Exception(sActionName + " Method not found in " + pcProcessContext.GetType().FullName + " class.");
					}

					ActionDelegate adActionDelegate = (ActionDelegate)Delegate.CreateDelegate(typeof(ActionDelegate), pcProcessContext, miMethodInfo, true);
					dActions.Add(sActionName, adActionDelegate);
				}

				dActions[sActionName]();
			}
		}

		public void ExecuteMessage(string sActionName, IProcessContext pcProcessContext, object oMessage)
		{
			if (!dMessageActions.ContainsKey(sActionName))
			{
				if (pcProcessContext == null)
				{
					throw new Exception("The process has no context associated.");
				}

				MethodInfo miMethodInfo = pcProcessContext.GetType().GetMethod(sActionName);

				if (miMethodInfo == null)
				{
					throw new Exception(sActionName + " Method not found in " + pcProcessContext.GetType().FullName + " class.");
				}

				MessageDelegate mdMessageDelegate = (MessageDelegate)Delegate.CreateDelegate(typeof(MessageDelegate), pcProcessContext, miMethodInfo, true);
				dMessageActions.Add(sActionName, mdMessageDelegate);
			}

			dMessageActions[sActionName](oMessage);
		}

		public void ExecuteException(string sActionName, IProcessContext pcProcessContext, Exception eException)
		{
			if (!dExceptionActions.ContainsKey(sActionName))
			{
				if (pcProcessContext == null)
				{
					throw new Exception("The process has no context associated.");
				}

				MethodInfo miMethodInfo = pcProcessContext.GetType().GetMethod(sActionName);

				if (miMethodInfo == null)
				{
					throw new Exception(sActionName + " Method not found in " + pcProcessContext.GetType().FullName + " class.");
				}

				ExceptionDelegate edExceptionDelegate = (ExceptionDelegate)Delegate.CreateDelegate(typeof(ExceptionDelegate), pcProcessContext, miMethodInfo, true);
				dExceptionActions.Add(sActionName, edExceptionDelegate);
			}

			dExceptionActions[sActionName](eException);
		}

		#endregion
	}
}
